import { login, logout, getInfo, loginCode, register } from '@/api/login'
import { getLogo, checkVipExpiry, getPackageInfo } from "@/api/user";
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getMessageList } from "../../api/user";
const user = {
  state: {
    token: getToken(),
    name: '',
    nickName: '',
    avatar: '',
    roles: [],
    userType: '',
    permissions: [],
    Logo: '',
    Title: '',
    loading: false,
    isVip: false,
    pageckAge: {},
    reader: false,
  },

  mutations: {
    SET_READER: (state, reader) => {
      state.reader = reader
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_NICK_NAME: (state, nickName) => {
      state.nickName = nickName
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_USERTYPE: (state, userType) => {
      state.userType = userType
    },
    SET_LOGO: (state, Logo) => {
      state.Logo = Logo
    },
    SET_TITLE: (state, Title) => {
      state.Title = Title
    },
    SET_LOADING: (state, loading) => {
      state.loading = loading
    },
    SET_ISVIP: (state, isVip) => {
      state.isVip = isVip
    },
    SET_PACKAGE: (state, pageckAge) => {
      state.pageckAge = pageckAge
    },

  },

  actions: {
    // 获取是否有已读
    // 获取通知消息
    getMessageList({ commit, state }) {
      let readed = ''
      let module = ''
      getMessageList({
        readed: readed,
        module: module,
      }).then((res) => {
        if (res.modules.length > 0) {
          for (var i = 0; i < res.modules.length; i++) {
            if (res.modules[i].readed !== 0) {
              return commit('SET_READER', true)
            } else {
              commit('SET_READER', false)
            }
          }
        } else {
          commit('SET_READER', false)
        }
      });
    },
    // 获取会员套餐信息
    getPackageInfo({ commit }) {
      getPackageInfo().then((res) => {
        commit('SET_PACKAGE', res)
      });
    },
    // 改变loading页面
    modifyLoading({ commit }, loading) {
      commit('SET_LOADING', loading)
    },
    // 储存token
    tokenTo({ commit }, token) {
      console.log(token);
      commit('SET_TOKEN', token)
    },
    // 注册登陆
    onRegister({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        register(userInfo).then(res => {
          setToken(res.token)
          commit('SET_TOKEN', res.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 验证码登陆
    codeLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        loginCode(userInfo).then(res => {
          setToken(res.token)
          commit('SET_TOKEN', res.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      const code = userInfo.code
      const uuid = userInfo.uuid
      const type = 2
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid, type).then(res => {
          setToken(res.token)
          commit('SET_TOKEN', res.token)
          resolve()
        }).catch(error => {
          console.log(error);
          reject(error)
        })
      })
    },
    async isVip({ commit, state }) {
      let res = await checkVipExpiry()
      commit('SET_ISVIP', res.data)
      localStorage.setItem('isVip', res.data)
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(res => {
          const user = res.user
          const avatar = user.avatar == "" ? require("@/assets/image/profile.jpg") : user.avatar.substr(0, 4) == "http" ? user.avatar : process.env.VUE_APP_BASE_API + user.avatar;
          if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_NAME', user.userName)
          commit('SET_NICK_NAME', user.nickName)
          commit('SET_AVATAR', avatar)
          commit('SET_USERTYPE', user.userType)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取企业logo以及企业简称
    getLogo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getLogo().then((res) => {
          commit('SET_LOGO', res.data.logo)
          commit('SET_TITLE', res.data.abbreviation)
        });
      })
    },
    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
